package cn.kgc.controller;

import cn.kgc.pojo.DevUser;
import cn.kgc.service.DevUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class DevUserController {
        @Resource
        private DevUserService devUserService;

        @RequestMapping("/action")
        public String index(){
            return "login";
        }

        @RequestMapping(value="login",method = RequestMethod.POST)
        public String doLogin(@RequestParam(value = "devcode") String devcode, @RequestParam("devPassword") String devPassword, HttpServletRequest request) throws Exception {

            System.out.println(devcode+""+devPassword);
          DevUser user= devUserService.devloginname(devcode, devPassword);

          if(null==user){
              request.setAttribute("loginMessage","用户名或者密码不正确");

              return "devlogin";
          }
          request.getSession().setAttribute("loginUser",user);

          return "developer/index";
        }

    /**
     * 注销
     * @param httpSession
     * @param request
     * @return
     */
        @RequestMapping(value="/logout")
        public String logout(HttpSession httpSession,HttpServletRequest request){
            String str="登陆失败";
            if(httpSession!=null){
                if(httpSession.getAttribute("loginUser")!=null){
                    httpSession.removeAttribute("loginUser");
                    return "login";
                }

            }
            request.setAttribute("logout",str);
                return "error1";
        }



}
